<?php
/**
 * Created by PhpStorm.
 * User: zhangligang
 * Date: 17/5/8
 * Time: 下午8:06
 */

namespace App\Http\Controllers;


use App\Http\Models\Color;
use App\Http\Models\Language;
use Illuminate\Http\Request as HttpRequest;

class SetController extends BaseController
{

    public function __construct()
    {
        $this->view = ['activeFirst' => 'set', 'activeSecond' => 'index'];
    }

    public function index()
    {
        return view('set.index', $this->view);
    }

    public function about()
    {

        $this->view['activeSecond'] = 'about';

        return view('set.about', $this->view);
    }

    public function language()
    {

        $languageList = Language::all(['id', 'title', 'short']);

        $this->view['activeFirst'] = 'language';
        
        return view('set.language', array_merge(['languageList' => $languageList], $this->view));
    }

    public function languageCreate()
    {

        $language = new Language();

        $this->view['activeFirst'] = 'language';

        return view('set.language-edit', array_merge(['language' => $language], $this->view));
    }

    public function languageUpdate($id)
    {

        $language = Language::query()->find($id);

        $this->view['activeFirst'] = 'language';

        return view('set.language-edit', $this->view);
    }

    public function languageSave(HttpRequest $request)
    {

        $languageArray = $request->all();

        $language = Language::firstOrcreate($languageArray);

        return redirect('language.html');
    }

    public function languageDelete(HttpRequest $request)
    {

        $id = (int)$request->get('id', 1);

        $code = 200;

        $message = '删除成功';

        if (Language::query()->where('id', '=', $id)->delete() === false) {

            $code = 404;

            $message = '删除失败,请重试';
        }

        return $this->send($code, $message);
    }

    public function color()
    {

        $colorList = Color::all(['id', 'name']);

        $this->view['activeFirst'] = 'color';

        return view('set.color', array_merge(['colorList' => $colorList], $this->view));
    }

    public function colorCreate()
    {

        $color = new Color();

        $this->view['activeFirst'] = 'color';

        return view('set.color-edit', array_merge(['color' => $color], $this->view));
    }

    public function colorUpdate($id)
    {

        $color = Color::query()->find($id);

        $this->view['activeFirst'] = 'color';

        return view('set.color-edit', $this->view);
    }

    public function colorSave(HttpRequest $request)
    {

        $colorArray = $request->all();

        $color = Color::firstOrcreate($colorArray);

        return redirect('color.html');
    }

    public function colorDelete(HttpRequest $request)
    {

        $id = (int)$request->get('id', 1);

        $code = 200;

        $message = '删除成功';

        if (Color::query()->where('id', '=', $id)->delete() === false) {

            $code = 404;

            $message = '删除失败,请重试';
        }

        return $this->send($code, $message);
    }


}